﻿/**************************************************************************
创建时间:	2020/5/15
作	  者:	张存
邮 	  箱:	zhangcunliang@126.com

Copyright (c) zhcun.cn

描	述：
记	录：
***************************************************************************/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ZhCun.DBModel;
using ZhCun.Utils;
using ZhCun.Utils.Helpers;
using ZhCun.Win.Controls;
using ZhCun.Win.Extend;

namespace ZhCun.Win.MenuRole
{
    public partial class FrmRoleMenuEdit : FrmBaseEdit
    {
        public FrmRoleMenuEdit()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 初始化treeview的方法,加载所有菜单
        /// </summary>
        public Action<TreeViewEx> InitMenuTreeHandle;

        void InitListView()
        {
            txtControlRights.Visible = false;
            lvControlRights.Visible = true;
            lvControlRights.Items.Clear();
            string menuName = txtMenuName.Text;

            if (menuName.IsEmpty())
            {
                return;
            }
            var rightsForm = CreateForm(menuName);  //ObjectFactory.GetObject<Form>(menuName);

            Dictionary<string, bool> rights = new Dictionary<string, bool>();
            string rightsArgs = txtControlRights.Text;
            if (!rightsArgs.IsEmpty())
            {
                var argArr = rightsArgs.SplitByNotEmpty(";");
                foreach (var item in argArr)
                {
                    var itemArr = item.SplitByNotEmpty(":");
                    rights.Add(itemArr[0], itemArr[1].StringEquals("true"));
                }
            }
            StringBuilder sb = new StringBuilder();
            rightsForm.FindToolStrip(FindAfter);

            bool FindAfter(Control c)
            {
                var ts = c as ToolStrip;
                foreach (ToolStripItem item in ts.Items)
                {
                    if (item.Owner.Parent is UcPageNav) continue;
                    if (!(item is ToolStripButton tsBtn)) continue;
                    if (tsBtn.Enabled) continue; //可用的不做处理
                    var itemText = tsBtn.Text == string.Empty ? tsBtn.Name : tsBtn.Text;
                    itemText = $"{ts.Text}:{tsBtn.Text}";

                    sb.Clear();
                    sb.Insert(0, $"{ts.Name}.{tsBtn.Name}");
                    Control ctr = ts.Parent;
                    while (ctr != rightsForm)
                    {
                        sb.Insert(0, $"{ctr.Name}.");
                        ctr = ctr.Parent;
                    }
                    sb.Insert(0, "A-");

                    ListViewItem lvi = new ListViewItem(itemText);
                    lvi.Name = sb.ToString();
                    if (rights.TryGetValue(lvi.Name, out bool val))
                    {
                        lvi.Checked = true;
                    }
                    lvControlRights.Items.Add(lvi);
                }
                return false;
            }

        }

        void OnSelectedIcon(string iconFile)
        {
            if (iconFile.IsEmpty())
            {
                return;
            }
            var fullPath = Path.Combine(Application.StartupPath, Config.Settings.IconFolder, iconFile);
            if (!File.Exists(fullPath))
            {
                return;
            }
            txtTooIcon.Text = iconFile;
            pBoxTooIcon.Image = Image.FromFile(fullPath);
        }

        private void FrmRoleMenuEdit_Load(object sender, EventArgs e)
        {
            OnSelectedIcon(txtTooIcon.Text);
            //InitListView();
        }

        private void btnSelectMenu_Click(object sender, EventArgs e)
        {
            FrmSelectTree frm = new FrmSelectTree();
            frm.InitTreeHandle = InitTreeView;
            frm.OnSelected = SelectedHandle;

            frm.ShowDialog();

            void InitTreeView(TreeViewEx tv)
            {
                InitMenuTreeHandle(tv);
                tv.IsSingleChecked = true;
            }

            ApiResult SelectedHandle(TreeView tv)
            {
                var selNode = tv.SelectedNode;
                
                if (selNode == null)
                {
                    return ApiResult.RetErr("没有选中任何节点");
                }

                var selModel = selNode.Tag as Sys_Menu;
                if (selModel?.IsMenu != true)
                {
                    return ApiResult.RetErr("必须选择菜单");
                }                
                txtMenuName.Text = selModel.MenuName;
                txtMenuName.ValueItem = selModel.Id;
                txtCaption.Text = selModel.MenuCaption;
                txtPCaption.Text = selNode.Parent.FullPath;
                txtCaption.Focus();
                txtCaption.SelectAll();
                //InitListView();
                return ApiResult.RetOK();
            }
        }

        private void btnReadRights_Click(object sender, EventArgs e)
        {
            InitListView();
        }

        private void lvControlRights_Leave(object sender, EventArgs e)
        {
            var selItems = lvControlRights.CheckedItems;
            StringBuilder sb = new StringBuilder();
            foreach (ListViewItem item in selItems)
            {
                sb.AppendLine($"{item.Name}:{true};");
            }
            txtControlRights.Text = sb.ToString();
        }

        private void btnSelectToolIcon_Click(object sender, EventArgs e)
        {
            FrmSelectIcon frm = new FrmSelectIcon();
            frm.OnSelected = OnSelectedIcon;
            frm.ShowDialog();
        }

        private void pBoxTooIcon_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            txtTooIcon.Text = string.Empty;
            pBoxTooIcon.Image = null;
        }
    }
}
